<template>
	<view class="container">
		<view class="cu-card">
			<view class="cu-form-group margin-lr-lg border-b-line padding-0">
				<view class="title">登录帐号</view>
				<input data-key="phone" focus placeholder="请输入手机号码" v-model="phone" :disabled="true"/>
			</view>
<!-- 			<view class="cu-form-group margin-lr-lg border-b-line padding-0">
				<view class="title">验 证 码</view>
				<input data-key="smscode" focus placeholder=" 请输入验证码" v-model="code" />
				<button class="cu-btn text-red line" @click="getCode">{{message}}</button>
			</view> -->
			<view class="cu-form-group margin-lr-lg border-b-line padding-0">
				<view class="title">旧密码</view>
				<input data-key="oldpassword" password focus placeholder="请输入旧密码" v-model="oldpassword" />
			</view>
			<view class="cu-form-group margin-lr-lg border-b-line padding-0">
				<view class="title">设置密码</view>
				<input data-key="password" password focus placeholder="请设置6位数的密码" v-model="password" />
			</view>
			<view class="cu-form-group margin-lr-lg border-b-line padding-0">
				<view class="title">确认密码</view>
				<input data-key="repassword" password focus placeholder="请输入确认密码" v-model="repassword" />
			</view>
			<view class="cu-form-group margin-lr-lg padding-0 submit_btn">
				<button class="cu-btn" type="button" @click="doreset">完成修改</button>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				index: 0,
				type:'',
				phone: "",
				code:"",
				oldpassword:"",
				password:"",
				repassword:"",
				message: "获取验证码",
				time: this.helper.time
			};
		},
		onLoad: function (option) {
			this.type = option.type;
			if(this.type == 'login'){
				uni.setNavigationBarTitle({
					title: '修改登录密码'
				});
			}
			if(this.type == 'pay'){
				uni.setNavigationBarTitle({
					title: '修改支付密码'
				});
			}
			this.helper.checkLogin();
			this.getData();
		},
		methods: {
			getData:function(){
				//获取基础参数
				// uni.showLoading({
				// 	title:"数据加载中",
				// 	mask:true
				// })
				var info = uni.getStorageSync("userinfo");
				let timestamp = this.helper.now();
				let sign = this.helper.setSign(['username='+info.username,'token='+info.token,'timestamp='+timestamp]);
				this.helper.req({
					api:"getUserInfo",
					timestamp:timestamp,
					data:{
						username:info.username,
						token:info.token,
					},
					header:{sign:sign},
					method:'GET',
				}).then(res=>{
					this.phone = res.data.data.userinfo.phone;
					setTimeout(() => {
						uni.hideLoading();
					}, 300);
				})
			},
			// daojishi() {
			// 	let eventinterval = setInterval(() => {
			// 		this.message = this.time-- + "秒";
			// 		if (this.time <= 0) {
			// 			clearInterval(eventinterval);
			// 			this.message = "获取验证码";
			// 		}
			// 	}, 1000);
			// },
			// getCode() {
			// 	if(this.phone == ''){
			// 		uni.showToast({
			// 			title:"请输入手机号",
			// 			icon:"none"
			// 		})
			// 		return;
			// 	}
			// 	if (this.message != "获取验证码") {
			// 		return;
			// 	}
			// 	//短信验证码的获取
			// 	let timestamp = this.helper.now();
			// 	let sign = this.helper.setSign(["phone=" + this.phone, "timestamp=" + timestamp]);
			// 	this.helper.req({
			// 		api:"getcode",
			// 		timestamp:timestamp,
			// 		data:{
			// 			phone: this.phone
			// 		},
			// 		header:{
			// 			sign: sign
			// 		}
			// 	}).then(res=>{
			// 		if (this.message == "获取验证码") {
			// 			this.time = this.helper.time;
			// 			this.daojishi();
			// 		}
			// 	});
			// },
			doreset() {
				if(this.oldpassword == ''){
					uni.showToast({
						title:"请输入旧密码",
						icon:"none"
					})
					return;
				}
				if(this.password == ''){
					uni.showToast({
						title:"请输入新密码",
						icon:"none"
					})
					return;
				}
				if(this.repassword == ''){
					uni.showToast({
						title:"请输入确认密码",
						icon:"none"
					})
					return;
				}
				if(this.phone == ''){
					uni.showToast({
						title:"请输入手机号",
						icon:"none"
					})
					return;
				}
				// if(this.code == ''){
				// 	uni.showToast({
				// 		title:"请输入验证码",
				// 		icon:"none"
				// 	})
				// 	return;
				// }
				var info = uni.getStorageSync("userinfo");
				let timestamp = this.helper.now();
				let sign = this.helper.setSign(['type='+this.type,'code='+this.code,'old_password='+this.oldpassword,'username='+this.phone,'password='+this.password,'repassword='+this.repassword,'timestamp='+timestamp,'token='+info.token]);
				this.helper.req({
					api:"repasswd",
					timestamp:timestamp,
					data:{type:this.type,username:this.phone,code:this.code,old_password:this.oldpassword,password:this.password,repassword:this.repassword,token:info.token},
					header:{
						sign: sign
					}
				}).then(res=>{
					//密码重置成功
					uni.showToast({
						title: "密码设置成功",
						icon: "success"
					})
					setTimeout(() => {
						uni.hideLoading();
						uni.redirectTo({
							url: '/pages/user/userinfo',
						});
					}, 300);
				});
			}
		}
	}
</script>

<style lang="scss">
	page {
		background: #ffffff;
	}
	.cu-form-group{
		background: none;
	}
	.cu-form-group .title {
		min-width: 200upx;
	}
</style>
